package com.abbas.smartCity.ui.findJob;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.abbas.smartCity.App;
import com.abbas.smartCity.R;
import com.abbas.smartCity.base.BaseActivity;
import com.abbas.smartCity.bean.BaseBean;
import com.abbas.smartCity.bean.findJob.Company;
import com.abbas.smartCity.bean.findJob.JobList;
import com.abbas.smartCity.bean.findJob.QueryResume;
import com.abbas.smartCity.retroft.ICallback;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import retrofit2.Call;

public class JobDetailActivity extends BaseActivity {

    private TextView name;
    private TextView tag1;
    private TextView tag2;
    private TextView tag3;
    private TextView tag4;
    private TextView tag5;
    private TextView tag6;
    private TextView comName;
    private TextView des;
    private Button postResume;
    private JobList.RowsBean jobData;
    private Company.DataBean companyData;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_job_detail;
    }

    @Override
    protected String setCustomTitle() {
        return "职位详情";
    }

    @Override
    protected void initView() {
        name = (TextView) findViewById(R.id.name);
        tag1 = (TextView) findViewById(R.id.tag1);
        tag2 = (TextView) findViewById(R.id.tag2);
        tag3 = (TextView) findViewById(R.id.tag3);
        tag4 = (TextView) findViewById(R.id.tag4);
        tag5 = (TextView) findViewById(R.id.tag5);
        tag6 = (TextView) findViewById(R.id.tag6);
        comName = (TextView) findViewById(R.id.com_name);
        des = (TextView) findViewById(R.id.des);
        postResume = (Button) findViewById(R.id.post_resume);
    }

    @Override
    protected void initData() {
        final String data = getIntent().getStringExtra("data");
        jobData = new Gson().fromJson(data, JobList.RowsBean.class);

        name.setText(jobData.getName());
        tag1.setText("岗位职责:" + jobData.getObligation());
        tag2.setText("公司地址:" + jobData.getAddress());
        tag3.setText("薪资待遇:" + jobData.getSalary());
        tag4.setText("联系人:" + jobData.getContacts());
//        tag5.setText("工作年限:"+jobData.get);

        api.getCompany(jobData.getCompanyId()).enqueue(new ICallback<Company>() {
            @Override
            public void onSuccess(Call<Company> call, Company data) {
                companyData = data.getData();
                comName.setText(data.getData().getCompanyName());
                des.setText(data.getData().getIntroductory());
            }
        });
    }

    @Override
    protected void initEvent() {
        postResume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                api.getResume(App.getUser().getUserId()).enqueue(new ICallback<QueryResume>() {
                    @Override
                    public void onSuccess(Call<QueryResume> call, QueryResume data) {
                        if (data.getData() == null) {
                            startActivity(AddResumeActivity.class);
                        } else {
                            final Map<String, Object> map = getMap();
                            map.put("companyId", jobData.getCompanyId());
                            map.put("money", 3000);
                            map.put("startTime", new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(new Date()));
                            map.put("postId", jobData.getProfessionId());
                            map.put("postName", jobData.getName());
                            api.postResume(createRequestBody(map)).enqueue(new ICallback<BaseBean>() {
                                @Override
                                public void onSuccess(Call<BaseBean> call, BaseBean data) {
                                    showToast(data.getMsg());
                                }
                            });
                        }
                    }
                });
            }
        });
    }
}